require('type')
local ascii = require('ascii')


-- 纹理操作
local TextureOperator = {}
TextureOperator.texture = nullptr

function TextureOperator:file(filename)
    if not isString(filename) then return nullptr end
    return texture_file(ascii(filename))
end

function TextureOperator:create(w, h)
    if not isNumber(w) or not isNumber(h) or w < 1 or h < 1 then
        return nullptr
    end
    return texture_create(w, h)
end

function TextureOperator:target(w, h)
    if not isNumber(w) or not isNumber(h) or w < 1 or h < 1 then
        return nullptr
    end
    return texture_target(w, h)
end

function TextureOperator:set(texture, h)
    if isString(texture) then
        self.texture = self:file(texture)
    elseif isNil(h) then
        self.texture = texture
    else
        self.texture = self:create(texture, h)
    end
end

function TextureOperator:get()
    return self.texture
end

function TextureOperator:getSize()
    if self.texture == nullptr then return 0, 0 end
    return  texture_getWidth(self.texture), texture_getHeight(self.texture)
end

function TextureOperator:getWidth()
    if self.texture == nullptr then return 0 end
    return  texture_getWidth(self.texture)
end

function TextureOperator:getHeight()
    if self.texture == nullptr then return 0 end
    return  texture_getHeight(self.texture)
end

function TextureOperator:lock(readonly, left, top, width, height)
    if self.texture == nullptr then return nullptr end
    if not isBool(readonly) then readonly = true end
    if not isNumber(left) then left = 0 end
    if not isNumber(top) then top = 0 end
    if not isNumber(width) then width = 0 end
    if not isNumber(height) then height = 0 end
    local ptr = texture_lock(self.texture, readonly, left, top, width, height)
    self.pitch = texture_pitch()
    return ptr
end

function TextureOperator:unlock()
    if self.texture == nullptr then return end
    return texture_unlock(self.texture)
end


function TextureOperator:release()
    if self.texture == nullptr then return end
    texture_release(self.texture)
    self.texture = nullptr
end



return TextureOperator